<?php
class SearchAction extends CommonAction {

    public function index(){

        // coreseek高效查找
        import('ORG.Util.Sphinxapi');
        $sphi = new SphinxClient();
        $sphi->setServer('127.0.0.1', 9312);
        $sphi->SetConnectTimeout(3);
        $sphi->SetArrayResult(true);
        $sphi->SetMatchMode(SPH_MATCH_ALL);
        C('TOKEN_ON',false);
        $type = $this->_get('type');
        $keyword = $this->_get('keyword');
        if(isset($type) && isset($keyword)) {
            if($type == 1) {
                $res = $sphi->Query($keyword, 'info_search');
                $ids = '';
                foreach($res['matches'] as $key=>$value){
                    $ids .= $value['id'].',';
                }
                $ids = substr($ids, 0 , -1);
                $info = M('Info');
                $map['i.id'] = array('in', $ids);
                //本类搜索
                if(isset($_GET['catid'])) {
                    $nav = M('InfoNav');
                    $pid = $nav->where('id='.$_GET['catid'])->getField('pid');
                    //如果是父类
                    if($pid == 0) {
                        $childIds = '';
                        $childArr = $nav->field('id')->where('pid='.$_GET['catid'])->select();
                        foreach($childArr as $key=>$value) {
                            $childIds .= $value['id'].',';
                        }
                        $childIds = substr($childIds, 0, -1);
                        $map['i.cid'] = array('in', $childIds);
                    } else {          //如果是子类
                        $map['i.cid'] = array('eq', $_GET['catid']);
                    }
                }
                $count = $info->alias('i')->where($map)->count();
                parent::page($count);
                $content = $info->alias('i')
                                ->field(array('i.id', 'i.title', 'i.content', 'iv.name as nav', 'iv.id as navid', 'i.addtime'))
                                ->join('xinxi_info_nav as iv ON i.cid=iv.id')
                                ->where($map)
                                ->limit($this->limit)
                                ->order('addtime DESC')
                                ->select();
                $opts=array(
                    'before_match'=>'<span style="font-weight:bold;color:#f00;">',
                    'after_match'=>'</span>'
                );
                foreach($content as $key=>&$value)
                {
                    $value = $sphi->BuildExcerpts($value, 'info_search', $keyword, $opts);
                }
            } else if($type == 2) {
                $news = M('News');
                $map['n.title|n.content|n.summary'] = array(array('like', '%'.$keyword.'%'), array('like',  '%'.$keyword.'%'), '_multi'=>true);
                $count = $news->alias('n')->where($map)->count();
                parent::page($count);
                $content = $news->alias('n')
                                ->field(array('n.id', 'n.title', 'n.summary', 'pv.name as nav', 'pv.id as navid', 'n.addtime'))
                                ->join('xinxi_press_nav as pv ON n.nav=pv.id')
                                ->where($map)
                                ->limit($this->limit)
                                ->order('addtime DESC')
                                ->select();

            }

            $this->assign(array(
                'count'=>$count,
                'content'=>$content
            ));
            $this->display();
        }
    }

}

?>